// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.liuxinlong.annotation.DataSource;
import com.liuxinlong.multiple.DataSourceContextHolder;

import lombok.extern.slf4j.Slf4j;

/**
 * 类描述
 * <p>
 * 此类关联设计的功能
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年2月22日
 */
@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect {
	@Pointcut("@within(com.liuxinlong.annotation.DataSource) || @annotation(com.liuxinlong.annotation.DataSource)")
	public void pointCut() {

	}

	@Before("pointCut() && @annotation(dataSource)")
	public void doBefore(DataSource dataSource) {
		log.info("切换数据源---" + dataSource.value().getValue());
		DataSourceContextHolder.setDataSource(dataSource.value().getValue());
	}

	@After("pointCut()")
	public void doAfter() {
		DataSourceContextHolder.clear();
	}
}
